package com.zhen.Aop;

import com.zhen.Mapper.OperateMapper;
import com.zhen.User.Admin;
import com.zhen.User.Operate;
import com.zhen.Utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.annotations.Insert;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Component
@Aspect//切面
public class Caozuo {
    @Autowired
    private OperateMapper operateMapper;
    @Autowired
    private HttpServletRequest request;
    @Around("@annotation(com.zhen.My.MyOperate)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Long time1=System.currentTimeMillis();
        String jwt = request.getHeader("token");
        Claims claims= JwtUtils.jietoken(jwt);
        String name =(String) (claims.get("id")+"");//操作人
        System.out.println("------------");
        System.out.println("id:"+claims.getId());
        System.out.println("ids:"+claims.get("id"));
        System.out.println("claims:"+claims);
        LocalDateTime operateTime =LocalDateTime.now();//操作时间
        String className = joinPoint.getSignature().getDeclaringTypeName();//操作类名
        String methodName = joinPoint.getSignature().getName();//方法名
        //调用原来方法执行
        Object result = joinPoint.proceed();
        Long time2=System.currentTimeMillis();
        Long time3= time2-time1;
        Operate operate=new Operate(null,name,operateTime,className,methodName,time3);
        operateMapper.insert(operate);
        return result;
    }
}
